package com.zzh.structuredeployment.activity

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.zzh.bridge_annotation.RouteTag
import com.zzh.custom.dialog.LevelSelectDialog
import com.zzh.structuredeployment.R
import com.zzh.structuredeployment.bean.SelectTestBean
import com.zzh.structuredeployment.bridge.BridgeApp
import kotlinx.android.synthetic.main.activity_selector.*


@RouteTag(BridgeApp.SELECTOR)
class SelectorActivity : AppCompatActivity() {

    private var mLevelSelectDialog: LevelSelectDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_selector)
        one.setOnClickListener {
            mLevelSelectDialog?.let {
                it.dismiss()
            }
            mLevelSelectDialog = LevelSelectDialog.Builder().setDatas(testDatas(1)).setCount(1)
                .setSingleSelect(sinSelectOneLists)
                .setOnLevelSelectListener { datas, indexs ->
                    var beans = datas
                    var values = StringBuffer()
                    indexs.forEach {
                        var item = beans[it]
                        values.append(item.selectName)
                        beans = item.childSelectMoudles
                    }
                    Log.i("zhangzhihao", "1>value>>$values")
                    sinSelectOneLists = indexs
                }
                .builder(this@SelectorActivity)
            mLevelSelectDialog?.show()
        }
        two.setOnClickListener {
            mLevelSelectDialog?.let {
                it.dismiss()
            }
            mLevelSelectDialog = LevelSelectDialog.Builder().setDatas(testDatas(2)).setCount(2)
                .setSingleSelect(sinSelectTwoLists)
                .setOnLevelSelectListener { datas, indexs ->
                    var beans = datas
                    var values = StringBuffer()
                    indexs.forEach {
                        var item = beans[it]
                        values.append(item.selectName)
                        beans = item.childSelectMoudles
                    }
                    Log.i("zhangzhihao", "2>value>>$values")
                    sinSelectTwoLists = indexs
                }
                .builder(this@SelectorActivity)
            mLevelSelectDialog?.show()
        }
        three.setOnClickListener {
            mLevelSelectDialog?.let {
                it.dismiss()
            }
            mLevelSelectDialog = LevelSelectDialog.Builder().setDatas(testDatas(3)).setCount(3)
                .setSingleSelect(sinSelectThreeLists)
                .setOnLevelSelectListener { datas, indexs ->
                    var beans = datas
                    var values = StringBuffer()
                    indexs.forEach {
                        var item = beans[it]
                        values.append(item.selectName)
                        beans = item.childSelectMoudles
                    }
                    Log.i("zhangzhihao", "3>value>>$values")
                    sinSelectThreeLists = indexs
                }
                .builder(this@SelectorActivity)
            mLevelSelectDialog?.show()
        }

        four.setOnClickListener {
            mLevelSelectDialog?.let {
                it.dismiss()
            }
            mLevelSelectDialog =
                LevelSelectDialog.Builder().setDatas(testDatas(4)).setCount(4).setTitle("title")
                    .setMultiSelect(true)
                    .setMultiSelects(mulSelectLists)
                    .setOnLevelMultiSelectListener { mDatas, indexs ->
                        mulSelectLists = indexs
                        Log.i("zhangzhihao", ">>>>点击了确定>>>>>>>>>>>>>>>$indexs")
                    }
                    .builder(this@SelectorActivity)
            mLevelSelectDialog?.show()
        }
    }

    private var mulSelectLists: List<List<Int>>? = null
    private var sinSelectOneLists: List<Int>? = null
    private var sinSelectTwoLists: List<Int>? = null
    private var sinSelectThreeLists: List<Int>? = null

    private fun testDatas(cen: Int): ArrayList<SelectTestBean>? {
        if (cen <= 0) {
            return null
        }
        var list = arrayListOf<SelectTestBean>()
        for (i in 1..30) {
            var bean = SelectTestBean()
            bean.name = ">$cen>$i"
            bean.selectTestBeans = if (i % 2 != 0) null else testDatas(cen - 1)
            list.add(bean)
        }
        return list
    }

}